<?php
class Admin_ContentController extends Zend_Controller_Action
{
    /**
     * Content model object
     * @var object
     */
    protected $_content;

    /**
     * Category model object
     * @var object
     */
    protected $_category;

    /**
     * Current user identity data
     * @var object
     */
    protected $_user;

    /**
     * Acl model object
     * @var object
     */
    protected $_acl;

    protected $_department;

    public function init() {
        $this->_helper->layout()->setLayout('admin');

        $this->_user       = Zend_Auth::getInstance()->getIdentity();        
        $this->_content    = new Admin_Model_Content();
        $this->_category   = new Admin_Model_Category();
        $this->_acl        = new Application_Model_Acl();
        $this->_department = new Admin_Model_Department();
    }

    public function indexAction() {     
        $type = $this->_getParam('type');
        $availableCategoriesId = $this->_acl->getRoleAvailableCategoriesId($this->_user->roleId);
        //if type not empty - get only this type content
        if ($type) {            
            $content = $this->_content->getAvailableContent(
                $availableCategoriesId,
                $type
            );
        } else {
            $content = $this->_content->getAvailableContent($availableCategoriesId);
        }      
             
        $this->view->content = $content;
        
        $this->view->categories = $this->_category->getAvailableAndNotEmptyCategoryTypes(
            $availableCategoriesId
        );        
        $this->view->total = count($content);
    }

    public function addPostAction() {       
        if ($this->getRequest()->isPost()) {
            $post = $this->getRequest()->getPost();                     
            $requiredData = array(
                'category_id' => $post['category'],
                'title'       => $post['title'],
                'alias'       => $post['alias'],
                'date_create' => $post['date_create'],
                'text'        => $post['text']
            );            
            $data = $this->_content->validate($requiredData);                           
            if (!isset($data['errors'])) {
                switch ($post['preview_radio']) {
                    case 'photo':                                                                
                        $data['preview_mediadata'] = $this->_content->uploadPreviewImage(
                            new Zend_File_Transfer_Adapter_Http()
                        );
                        break;
                    case 'video':
                        $data['preview_mediadata'] = $post['video'];
                        break;                    
                }
                $data['is_visible']   = $post['visible'];                                
                $data['description']  = $post['description'];
                $data['keywords']     = $post['keywords'];
                $data['preview_text'] = $post['preview_text'];
                $data['author_id']    = $this->_user->id;                
                $postId = $this->_content->addPost($data);

                if (!empty($post['tags'])) {
                    $this->_content->savePostTags($post['tags'], $postId);
                }                                
                $this->_helper->redirector('edit-post', 'content', 'admin', array('post_id' => $postId));
            } else {
                $this->view->errors = $data['errors'];
            }           
        }
        
        $availableCategories = $this->_acl->getRoleAvailableCategoriesId(
            $this->_user->roleId
        );                    
        $this->view->categories = $this->_category->getAvailableForPostCategories(
            $availableCategories
        );
        $this->view->departments = $this->_department->getAvailableForRoleDepartments(
            $this->_user->roleId
        );
    }

    public function editPostAction() {        
        $postId = $this->_getParam('post_id');
        if ($this->getRequest()->isPost()) {
            $post = $this->getRequest()->getPost();            
            $requiredData = array(
                'category_id' => $post['category'],
                'title'       => $post['title'],                                                
                'text'        => $post['text'],
                'alias'       => $post['alias'],
                'date_create' => $post['date_create']
            );
            $data = $this->_content->validate($requiredData, $postId);                           
            if (!isset($data['errors'])) {
                if (isset($post['preview_radio'])) {
                    switch ($post['preview_radio']) {
                        case 'photo':                                                                
                            $data['preview_mediadata'] = $this->_content->uploadPreviewImage(
                                new Zend_File_Transfer_Adapter_Http()
                            );
                            break;
                        case 'video':
                            $data['preview_mediadata'] = $post['video'];
                            break;
                        default:
                            $data['preview_mediadata'] = '';
                            break;                    
                    }
                }
                $data['is_visible']   = $post['visible'];                                
                $data['description']  = $post['description'];
                $data['keywords']     = $post['keywords'];                                                
                $data['preview_text'] = $post['preview_text'];

                $this->_content->updatePost($data, $postId);
                $this->_content->deleteAssociateBetweenPostAndTags($postId);
                if (!empty($post['tags'])) {
                    $this->_content->savePostTags($post['tags'], $postId);
                }                
                //$this->_helper->redirector('index', 'content', 'admin');
            } else {
                $this->view->errors = $data['errors'];
            }        
        }
        
        $availableCategories = $this->_acl->getRoleAvailableCategoriesId(
            $this->_user->roleId
        );
        $this->view->categories = $this->_category->getAvailableForPostCategories(
            $availableCategories
        );
            
        $postData = $this->_content->getPostData($postId);        
        if (!preg_match('/iframe|object/', $postData['preview_mediadata'])
            && $postData['preview_mediadata']) {
            $image = $postData['preview_mediadata'];
            $postData['preview_mediadata'] = '<img width="600px" src="/public/uploads/preview_images/'.$image.'"></img>';
        }
        $this->view->data = $postData;                  

        $this->view->categoryDepartmentId = $this->_category->getCategoryDepartmentId(
            $postData['category_id']
        );             
        $this->view->departments = $this->_department->getAvailableForRoleDepartments(
            $this->_user->roleId
        );
        $this->view->category = $postData['category_id'];            
    }

    public function changeVisiblePostAction() {
        $postId = $this->_getParam('post_id');
        $status = intval($this->_getParam('status'));       
        $status = $status?$status-1:$status+1;  

        $this->_content->changeVisiblePost($status, $postId);
        $this->_helper->redirector('index', 'content', 'admin');
    }

    public function deletePostAction() {        
        $postId = $this->_getParam('post_id');        
        $this->_content->deletePost($postId);
        $this->_helper->redirector('index', 'content', 'admin');
    }
}